<template>
  <div class="QR-code">
    <div v-for="item in list" :key="item.m_priceid">
      <h3>{{item.priceDesc}}</h3>
      <van-button round type="info" class="btn" v-if="item.qrcodeUrl==null" @click="getQR(item)"
        >生成二维码</van-button
      >
      <div class="content" v-else>
        <div>
          <img :src="item.qrcodeUrl" alt="" />
          <p>{{item.priceDesc}}</p>
        </div>
        <div>
          <!-- <van-button round type="info" class="btn" style="margin-right:30px" @click="getQR(item)">重新生成</van-button> -->
          <van-button round type="warning" class="btn"  @click="downQr(item.qrcodeUrl)">保存二维码</van-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getBusinessConfigList, generateCode } from '../service/service.js'
export default {
  name: 'shopAssistant-add',
  data () {
    return {
      userId: '',

      list: []
    }
  },
  created () {
    this.userId = this.$route.query.userId
    this.init()
  },
  computed: {
    loginInfo () {
      return this.$store.state.channel.loginInfo
    }
  },
  methods: {
    init () {
      getBusinessConfigList({ shopUserId: this.userId })
        .then((res) => {
          const data = res.data
          if (data.ret !== 0) {
            this.$toast(data.msg)
            return false
          }
          this.list = data.data
        // this.$router.back()
        })
        .catch((err) => {
          console.log(err)
        })
    },
    getQR (val, index) {
      generateCode({ shopUserId: this.userId, mpriceid: val.m_priceid })
        .then((res) => {
          const data = res.data
          if (data.ret !== 0) {
            this.$toast(data.msg)
            return false
          }
          // this.$set(this.list[index], 'qrcodeUrl', data.qrcodeUrl)
          this.init()
        })
        .catch((err) => {
          console.log(err)
        })
    },
    downQr (qrcodeUrl) {
      // const eleLink = document.createElement('a')
      // eleLink.download = 'erCode.jpg'
      // eleLink.style.display = 'none'
      // eleLink.href = qrcodeUrl
      // document.body.appendChild(eleLink)
      // eleLink.click()
      // document.body.removeChild(eleLink)
      if (this.loginInfo.loginSrc === 0) {
        if (navigator.userAgent.indexOf('iPhone') > -1) {
          window.location.href = 'ios:savePicture00(' + qrcodeUrl + ')' // 调用ios的函数
        } else {
          window.injs.savePicture00(qrcodeUrl) // 通过injs接口调用android的函数
        }
        return
      }
      this.$saveAs(qrcodeUrl, 'image.jpg')
    }
  }
}
</script>
<style lang="less" scoped>
.QR-code {
  padding: 15px;
  box-sizing: border-box;
  > div {
    text-align: center;
    background: #fff;
    border-radius: 8px;
    padding: 15px 0 30px;
    margin-bottom: 15px;
    h3 {
      font-size: 20px;

      font-weight: 700;
      color: #333333;
      line-height: 28px;
    }
    .btn {
      padding: 0 30px;
      margin-top: 40px;
    }
  }
  .content {
    > div:first-child {
      width: 220px;
      border: 1px solid #dddddd;
      padding: 10px;
      text-align: center;
      display: inline-block;
      margin-top: 20px;
      > img {
        width: 100%;
      }
      > p {
        margin-top: 10px;
        color: #999;
      }
    }

  }
}
</style>
